package com.travelsky.app.security.support;

import java.util.List;
import java.util.Map;

import org.hibernate.FetchMode;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.travelsky.app.persistence.User;
import com.travelsky.app.security.EhCacheBasedResourceCache;
import com.travelsky.app.security.SecurityManager;

@Service("securityManager")
public class SecurityManagerSupport extends HibernateDaoSupport implements  UserDetailsService,SecurityManager {
    @Autowired
    private EhCacheBasedResourceCache ehCacheBasedResourceCache;
    @Autowired
    public void init(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }
    /**
     * (非 Javadoc)
     * 获取资源
     * 并且放入缓存中
     * @return
     * @see com.travelsky.app.security.SecurityManager#loadUrlAuthorities()
     */
    @Override
    public Map<String, String> loadUrlAuthorities() {
        Map<String, String> urlAuthorities;
        urlAuthorities = ehCacheBasedResourceCache.getUrlAuthoritiesFromCache();
        if (urlAuthorities == null) {
            urlAuthorities = ehCacheBasedResourceCache.putUrlAuthoritiesInCache();
        }
      /*  Map<String, String> urlAuthorities = new HashMap<String, String>(); 
        DetachedCriteria resourceCriteria = DetachedCriteria.forClass(Resource.class);
        resourceCriteria.add(Restrictions.eq("type", "URL"));
        List<Resource> urlResources = getHibernateTemplate().findByCriteria(resourceCriteria);  
           for(Resource resource : urlResources) {  
               urlAuthorities.put(resource.getValue(), resource.getRoleAuthorities());  
           }*/
        return urlAuthorities;
    }
    
    /**
     * (非 Javadoc)
     * 
     *  获取用户
     * @param userName
     * @return
     * @throws UsernameNotFoundException
     * @throws DataAccessException
     * @see org.springframework.security.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
     */
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException, DataAccessException {
       DetachedCriteria userCriteria = DetachedCriteria.forClass(User.class);
       //设置不为懒加载
       userCriteria.setFetchMode("roles", FetchMode.JOIN);
       //查询条件设置
       userCriteria.add(Restrictions.eq("name", userName)).add(Restrictions.eq("disabled", false));
       List<User> users  = getHibernateTemplate().findByCriteria(userCriteria);
       if (users.isEmpty()) {
           throw new UsernameNotFoundException("User " + userName + " has no GrantedAuthority");
       }
       return users.get(0);
    }

}
